package com.cqeec.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;

import com.cqeec.action.BookAction;
import com.cqeec.action.ReaderAction;
import com.cqeec.bean.BookInfo;
import com.cqeec.bean.ReaderInfo;

public class LendingMainPanel extends JPanel {
	String flag = "";
	JPanel lendPanel;
	JLabel lbReaderid,lbReaderName,lbReaderSex,lbBirthday,lbTel,lbBookid,
	       lbBookName,lbType,lbPrice,lbAuthor,lbPublishDate,lbPublisher;
	JTextField txtReaderid, txtReaderName, txtReaderSex, txtBirthday, txtTel;
	JTextField txtBookid, txtBookName, txtType, txtPrice, txtAuthor,
			txtPublishDate, txtPublisher;
	public LendingMainPanel(String flag) {
		this.flag = flag;
		LendingFrame.lbState.setText("状态：查询读者信息、图书信息后完成借还操作");
		LendingFrame.returnBtn.setEnabled(true);
		LendingFrame.borrowBtn.setEnabled(false);
		LendingFrame.backBtn.setEnabled(false);
		LendingFrame.optBtn.setEnabled(true);
		LendingFrame.findBtn.setEnabled(false);
		this.setLayout(null);
		initGUI();
	}
	
	// GUI界面设置
	private void initGUI() {
		try {
			lendPanel = new JPanel();
			String title = "图书借阅";
			if (flag.equals("back")){
				title = "图书归还";
			}
			lendPanel.setBorder(BorderFactory.createTitledBorder(null,
					title, TitledBorder.CENTER, TitledBorder.ABOVE_TOP,
					new Font("宋体", Font.BOLD, 14), Color.black));
			lendPanel.setBounds(80, 10, 490, 410);
			add(lendPanel);
			lendPanel.setLayout(null);
			lbReaderid = new JLabel();
			lendPanel.add(lbReaderid);
			lbReaderid.setText("读者编号:");
			lbReaderid.setBounds(50, 46, 72, 15);
			txtReaderid = new JTextField();
			lendPanel.add(txtReaderid);
			txtReaderid.setBounds(122, 43, 187, 22); 
			txtReaderid.setToolTipText("输入后直接回车查询");
			Document readerdt = txtReaderid.getDocument();
			readerdt.addDocumentListener(new DocumentListener(){                  
                public void insertUpdate(DocumentEvent e) {
                }
                public void removeUpdate(DocumentEvent e) {
                	txtReaderName.setText(""); 
					txtReaderSex.setText(""); 
					txtBirthday.setText("");
					txtTel.setText("");
                }
                public void changedUpdate(DocumentEvent e) {
                }
            });
			txtReaderid.addKeyListener(new KeyListener() {
				public void keyTyped(KeyEvent e) {
					// TODO Auto-generated method stub
				}
				public void keyReleased(KeyEvent e) {
					// TODO Auto-generated method stub
				}
				public void keyPressed(KeyEvent e) {
					// TODO Auto-generated method stub
					int keycode = e.getKeyCode();
					if (keycode == 10){
						if (!txtReaderid.getText().trim().equals("")){
							ReaderAction action = new ReaderAction();
							ReaderInfo readerInfo = action.getReaderInfoByID(txtReaderid.getText().trim());
							if (readerInfo == null){
								JOptionPane.showMessageDialog(null, "读者编号有误,请重新输入", "读者信息",JOptionPane.WARNING_MESSAGE);
								txtReaderid.requestFocusInWindow();
								txtReaderName.setText(""); 
								txtReaderSex.setText(""); 
								txtBirthday.setText("");
								txtTel.setText("");
							}else{
								txtReaderName.setText(readerInfo.getReadername());
								txtReaderSex.setText(readerInfo.getSex());
								txtTel.setText(readerInfo.getTel());
								txtBirthday.setText(readerInfo.getBirthday());
							}
						}
					}
				}
			});
			
			lbReaderName = new JLabel();
			lendPanel.add(lbReaderName);
			lbReaderName.setText("读者姓名:");
			lbReaderName.setBounds(50, 86, 72, 15);
			txtReaderName = new JTextField();
			lendPanel.add(txtReaderName);
			txtReaderName.setBounds(122, 83, 187, 22); 
			txtReaderName.setEditable(false);
			
			lbReaderSex = new JLabel();
			lendPanel.add(lbReaderSex);
			lbReaderSex.setText("读者性别:");
			lbReaderSex.setBounds(325, 86, 72, 15);
			txtReaderSex = new JTextField();
			lendPanel.add(txtReaderSex);
			txtReaderSex.setBounds(398, 83, 40, 22); 
			txtReaderSex.setEditable(false);
			
			lbBirthday = new JLabel();
			lendPanel.add(lbBirthday);
			lbBirthday.setText("出生日期:");
			lbBirthday.setBounds(50, 126, 72, 15);
			txtBirthday = new JTextField();
			lendPanel.add(txtBirthday);
			txtBirthday.setBounds(122, 123, 100, 22); 
			txtBirthday.setEditable(false);
			
			lbTel = new JLabel();
			lendPanel.add(lbTel);
			lbTel.setText("联系电话:");
			lbTel.setBounds(240, 126, 72, 15);
			txtTel = new JTextField();
			lendPanel.add(txtTel);
			txtTel.setBounds(315, 123, 123, 22); 
			txtTel.setEditable(false);
			
			lbBookid = new JLabel();
			lendPanel.add(lbBookid);
			lbBookid.setText("图书编号:");
			lbBookid.setBounds(50, 196, 72, 15);
			txtBookid = new JTextField();
			lendPanel.add(txtBookid);
			txtBookid.setBounds(122, 193, 187, 22); 
			txtBookid.setToolTipText("输入后直接回车查询");
			Document bookdt = txtBookid.getDocument();
			bookdt.addDocumentListener(new DocumentListener() {
				public void removeUpdate(DocumentEvent e) {
					// TODO Auto-generated method stub
					txtBookName.setText("");
					txtType.setText(""); 
					txtPrice.setText(""); 
					txtAuthor.setText("");
					txtPublishDate.setText(""); 
					txtPublisher.setText("");
				}
				
				public void insertUpdate(DocumentEvent e) {
					// TODO Auto-generated method stub
				}
				
				public void changedUpdate(DocumentEvent e) {
					// TODO Auto-generated method stub
				}
			});
			txtBookid.addKeyListener(new KeyListener() {
				public void keyTyped(KeyEvent e) {
					// TODO Auto-generated method stub
					
				}
				
				public void keyReleased(KeyEvent e) {
					// TODO Auto-generated method stub
					
				}
				public void keyPressed(KeyEvent e) {
					// TODO Auto-generated method stub
					int keycode = e.getKeyCode();
					if (keycode == 10){
						if (!txtBookid.getText().trim().equals("")){
							BookAction action = new BookAction();
							BookInfo bookInfo = action.getBookInfoByID(txtBookid.getText().trim());
							if (bookInfo == null){
								JOptionPane.showMessageDialog(null, "图书编号有误,请重新输入", "图书信息",JOptionPane.WARNING_MESSAGE);
								txtBookid.requestFocusInWindow();
								txtBookName.setText("");
								txtType.setText(""); 
								txtPrice.setText(""); 
								txtAuthor.setText("");
								txtPublishDate.setText(""); 
								txtPublisher.setText("");
							}else{
								txtBookName.setText(bookInfo.getBookname());
								txtType.setText(bookInfo.getBooktype()); 
								txtPrice.setText(bookInfo.getPrice()); 
								txtAuthor.setText(bookInfo.getAuthor());
								txtPublishDate.setText(bookInfo.getPublishdate()); 
								txtPublisher.setText(bookInfo.getPublisher());
							}
						}
					}
				}
			});
			
			lbBookName = new JLabel();
			lendPanel.add(lbBookName);
			lbBookName.setText("图书名称:");
			lbBookName.setBounds(50, 236, 72, 15);
			txtBookName = new JTextField();
			lendPanel.add(txtBookName);
			txtBookName.setBounds(122,233, 187, 22); 
			txtBookName.setEditable(false);
			
			lbPrice = new JLabel();
			lendPanel.add(lbPrice);
			lbPrice.setText("图书价格:");
			lbPrice.setBounds(325, 236, 72, 15);
			txtPrice = new JTextField();
			lendPanel.add(txtPrice);
			txtPrice.setBounds(398, 233, 40, 22); 
			txtPrice.setEditable(false);
			
			lbPublisher = new JLabel();
			lendPanel.add(lbPublisher);
			lbPublisher.setText("出版社:");
			lbPublisher.setBounds(50, 276, 72, 15);
			txtPublisher = new JTextField();
			lendPanel.add(txtPublisher);
			txtPublisher.setBounds(122,273, 147, 22); 
			txtPublisher.setEditable(false);
			
			lbPublishDate = new JLabel();
			lendPanel.add(lbPublishDate);
			lbPublishDate.setText("出版日期:");
			lbPublishDate.setBounds(285, 276, 72, 15);
			txtPublishDate = new JTextField();
			lendPanel.add(txtPublishDate);
			txtPublishDate.setBounds(358, 273, 80, 22); 
			txtPublishDate.setEditable(false);
			
			lbAuthor = new JLabel();
			lendPanel.add(lbAuthor);
			lbAuthor.setText("图书作者:");
			lbAuthor.setBounds(50, 316, 72, 15);
			txtAuthor = new JTextField();
			lendPanel.add(txtAuthor);
			txtAuthor.setBounds(122,313, 147, 22); 
			txtAuthor.setEditable(false);
			
			lbType = new JLabel();
			lendPanel.add(lbType);
			lbType.setText("图书类型:");
			lbType.setBounds(285, 316, 72, 15);
			txtType = new JTextField();
			lendPanel.add(txtType);
			txtType.setBounds(358, 313, 80, 22); 
			txtType.setEditable(false);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
